package zhoug.demo.zui.ui;

import android.os.Bundle;

import java.util.Arrays;

import androidx.appcompat.app.AppCompatActivity;
import zhoug.demo.zui.R;
import zhoug.zui.dialog.list.ZUISelectListDialog;
import zhoug.zui.ints.OnSelectedListener;
import zhoug.zui.menu.horizontal.ZUIMenuView;

public class MenuGroupActivity extends AppCompatActivity {
    private ZUIMenuView mZUIMenuView1;
    private int selected1=-1;
    private ZUIMenuView mZUIMenuView2;
    private ZUIMenuView mZUIMenuViewItem1;
    private ZUIMenuView mZUIMenuViewItem2;
    private ZUIMenuView mZUIMenuViewItem3;
    private ZUIMenuView mZUIMenuViewItem4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_group);
        findViews();


        mZUIMenuView1.setOnExpandChangeListener(new ZUIMenuView.OnExpandChangeListener() {
            @Override
            public void onExpand(boolean expand) {
                showDialog(1,expand);
            }
        });

        mZUIMenuView2.setOnExpandChangeListener(new ZUIMenuView.OnExpandChangeListener() {
            @Override
            public void onExpand(boolean expand) {
                showDialog(2,expand);
            }
        });
        mZUIMenuViewItem1.setOnExpandChangeListener(new ZUIMenuView.OnExpandChangeListener() {
            @Override
            public void onExpand(boolean expand) {
                showDialog(11,expand);
            }
        });
        mZUIMenuViewItem2.setOnExpandChangeListener(new ZUIMenuView.OnExpandChangeListener() {
            @Override
            public void onExpand(boolean expand) {
                showDialog(12,expand);
            }
        });
        mZUIMenuViewItem3.setOnExpandChangeListener(new ZUIMenuView.OnExpandChangeListener() {
            @Override
            public void onExpand(boolean expand) {
                showDialog(13,expand);
            }
        });
        mZUIMenuViewItem4.setOnExpandChangeListener(new ZUIMenuView.OnExpandChangeListener() {
            @Override
            public void onExpand(boolean expand) {
                showDialog(14,expand);
            }
        });

    }

    private void findViews() {
        mZUIMenuView1 = findViewById(R.id.ZUIMenuView1);
        mZUIMenuView2 = findViewById(R.id.ZUIMenuView2);
        mZUIMenuViewItem1 = findViewById(R.id.ZUIMenuView_item1);
        mZUIMenuViewItem2 = findViewById(R.id.ZUIMenuView_item2);
        mZUIMenuViewItem3 = findViewById(R.id.ZUIMenuView_item3);
        mZUIMenuViewItem4 = findViewById(R.id.ZUIMenuView_item4);
    }

    private ZUISelectListDialog<String> dialog;

    private void showDialog(int tag,boolean expand){
        if(expand){
            if(dialog==null){
                dialog=new ZUISelectListDialog<String>(this) {
                    @Override
                    public String getText(String itemData) {
                        return itemData;
                    }
                };
                dialog.setData(Arrays.asList("重庆","上海","背景","乌鲁木齐","孝哉闵子骞鞭打芦bai花车牛返村","四川成都市武侯区"));
                dialog.setSelected(selected1);
                dialog.setOnSelectedListener(new OnSelectedListener() {
                    @Override
                    public void onSelected(int selected) {
                        selected1=selected;
                        int tag1 = (int) dialog.getTag();
                        if(tag1==1){
                            mZUIMenuView1.setMenuName(dialog.getItemData(selected));
                            mZUIMenuView1.setExpanded(false);
                        }else  if(tag1==2){
                            mZUIMenuView2.setMenuName(dialog.getItemData(selected));
                            mZUIMenuView2.setExpanded(false);
                        }else  if(tag1==11){
                            mZUIMenuViewItem1.setMenuName(dialog.getItemData(selected));
                            mZUIMenuViewItem1.setExpanded(false);
                        }else  if(tag1==12){
                            mZUIMenuViewItem2.setMenuName(dialog.getItemData(selected));
                            mZUIMenuViewItem2.setExpanded(false);
                        }else  if(tag1==13){
                            mZUIMenuViewItem3.setMenuName(dialog.getItemData(selected));
                            mZUIMenuViewItem3.setExpanded(false);
                        }else  if(tag1==14){
                            mZUIMenuViewItem4.setMenuName(dialog.getItemData(selected));
                            mZUIMenuViewItem4.setExpanded(false);
                        }


                    }
                });
            }
            dialog.setTag(tag);
            if(!dialog.isShowing()){
                dialog.show();
            }
        }else {
            if(dialog!=null && dialog.isShowing()){
                dialog.dismiss();
            }
        }
    }
}